﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;
using System.IO;
using System.Windows.Threading;

namespace SlvPartecipazione
{
    public delegate void VerificaVittoriaDelegate();
    public delegate void DownloadXmlPartecipanti(string Xml);

    public class Invoker
    {
        public static event VerificaVittoriaDelegate VerificaVittoriaEvent;
        public static event DownloadXmlPartecipanti DownloadXmlPartecipantiEvent;

        private const string myUrl = "http://www.sanitelitalia.it/ASPX/SlvAjax.aspx";
        private const string myUrlXml = "http://www.sanitelitalia.it/TEST/ListaPartecipanti.xml";
        private WebClient client;
        private string myGuid;

        private bool invokeVerificaVittoria = false;
        public bool InvokeVerificaVittoria
        {
            get { return this.invokeVerificaVittoria; }
            set { this.invokeVerificaVittoria = value; }
        }

        private bool invokePartecipantiEvent = false;
        public bool InvokePartecipantiEvent
        {
            get { return this.invokePartecipantiEvent; }
            set { this.invokePartecipantiEvent = value; }
        }

        private string getUrl()
        {
            return myUrl + "?GUID=" + myGuid + "&";
        }

        public Invoker()
        {
            myGuid = Utils.getGUID();

            client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
        }

        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                if (e.Error == null)
                {
                    if (invokeVerificaVittoria)
                    {
                        int numeroVittorie = 0;

                        if (int.TryParse(e.Result, out numeroVittorie))
                        {
                            if (numeroVittorie > 0)
                            {
                                VerificaVittoriaEvent.Invoke();
                            }
                        }
                    }
                    else if (invokePartecipantiEvent)
                    {
                        DownloadXmlPartecipantiEvent.Invoke(e.Result);
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

        public void IncrementaAiuto()
        {
            try
            {
                if(!string.IsNullOrEmpty(myGuid))
                    client.DownloadStringAsync(new Uri(getUrl() + "&OP=INC_AIUTO"));
            }
            catch (Exception ex)
            { }
        }

        public void IncrementaDurata(int durataToAdd)
        {
            try
            {
                if (!string.IsNullOrEmpty(myGuid))
                    client.DownloadStringAsync(new Uri(getUrl() + "&OP=INC_DURATA&DURATA=" + durataToAdd));
            }
            catch (Exception ex)
            { }
        }

        public void CreaPartita()
        {
            try
            {
                if (!string.IsNullOrEmpty(myGuid))
                    client.DownloadStringAsync(new Uri(getUrl() + "OP=CREA_PARTITA"));
            }
            catch (Exception ex)
            { }
        }

        public void SegnalaVittoria()
        {
            try
            {
                if (!string.IsNullOrEmpty(myGuid))
                    client.DownloadStringAsync(new Uri(getUrl() + "OP=SEGNALA_VITTORIA"));
            }
            catch (Exception ex)
            { }
        }

        public void VerificaVittoria()
        {
            try
            {
                if (!string.IsNullOrEmpty(myGuid))
                    client.DownloadStringAsync(new Uri(getUrl() + "OP=VERIFICA_VITTORIA"));
            }
            catch (Exception ex)
            { }
        }

        public void DownloadXmlPartecipanti()
        {
            try
            {
                client.DownloadStringAsync(new Uri(myUrlXml));
            }
            catch (Exception ex)
            { }
        }
    }
}
